package com.hejjon.controller;

import com.hejjon.core.bean.ResponseResult;
import com.hejjon.core.bean.StrListBean;
import com.hejjon.core.controller.BaseController;
import com.hejjon.core.domain.entity.Connection;
import com.hejjon.service.IConnectionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据库连接
 * @author: cs
 * @date: 2025-02-20 14:20:40
 * @since: 1.0
 */
@RestController
@RequestMapping("/datasource/connection")
public class ConnectionController extends BaseController {

    @Resource
    private IConnectionService connectionService;

    @PostMapping("/create")
    public ResponseResult create(@RequestBody Connection conn) {

        boolean state = connectionService.createConn(conn);
        return toResult(state, "创建成功");
    }

    @GetMapping("/list")
    public ResponseResult list() {

        List<Connection> connectionList = connectionService.findConnectionList();
        return success(connectionList);
    }


    @PostMapping("/testConnect")
    public ResponseResult testConnect(@RequestBody Connection conn) {

        boolean succ = connectionService.testConnect(conn);
        return toResult(succ, "连接成功");
    }


    @PostMapping("/update")
    public ResponseResult update(@RequestBody Connection connection) {
        boolean succ = connectionService.update(connection);
        return toResult(succ, "更新成功");
    }


    /**
     * 批量删除
     * @param req
     * @return
     */
    @PostMapping("/batchRemove")
    public ResponseResult batchRemove(@RequestBody StrListBean req) {

        return toResult(connectionService.removeByIds(req.getIds()), "连接删除成功!");
    }

}
